package com.tron.dobest.java.tronangent;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class SimpleCGLibDemo {

    static class RealService {
        public void sayHello() {
            System.out.println("真实服务sayHello");
        }
    }

    static class SimpleInterceptor implements MethodInterceptor {
        @Override
        public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
            System.out.println("代理方法start");
            Object result = methodProxy.invokeSuper(o, objects);
            System.out.println("代理方法end");
            return result;
        }
    }

    public static void main(String[] args) {
        RealService proxyService = (RealService) Enhancer.create(RealService.class, new SimpleInterceptor());
        proxyService.sayHello();
    }

}
